package com.aos.config;

import com.aos.exception.BusinessException;
import com.aos.exception.ResultStatus;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DateConverter
 * @Description:接口日期参数转换器
 * @Author wumingwang
 * @Date 2018/9/30 16:44
 * @Version 1.0
 */
public class DateConverter implements Converter<String,Date> {
    private SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date convert(String s) {
        if (s == null || "".equals(s.trim())) {
            return null;
        }
        try {
            s = s.trim();
            if(s.length() == 10){
                return simpleDate.parse(s);
            }else{
                return simpleTime.parse(s);
            }
        } catch (ParseException e) {
            throw new BusinessException("日期格式错误!", ResultStatus.fail.toString());
        }
    }
}
